
use std::fmt;

// #[derive(debug)]
struct AppErr {
    code: usize,
    msg: String,
}

impl fmt::Display for AppErr {
    fn fmt(&self,f:&mut fmt::Formatter) -> fmt::Result {
        let err_msg = match self.code {
            404 => self.code.to_string() + ", Sorry, can not find the page!",
            _ => "sorry, something is wrong, please try again.".to_string(),
        };

        write!(f, "{}", err_msg)
    }
}

impl fmt::Debug for AppErr {
    fn fmt(&self,f:&mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "[mydebug] AppErr{{ code:{}, message:{} }}",
            self.code, self.msg
        )
    }
}

fn process_error() -> Result<(), AppErr> {
    Err(AppErr{
        code: 404,
        msg: String::from("page not found"), // msg: "page not found".to_string(),
    })
}

fn main() {
    // println!("Hello, world!");
    println!("1");
    match process_error() {
        Err(e) => eprintln!("{}", e),
        _ => println!("meet error, please try again"),
    };
    println!("2");
    eprintln!("{:?}", process_error());
    println!("3");
    eprintln!("{:#?}", process_error());
        
}
